import { _decorator, Component, EditBox, EventKeyboard, game, Game, Input, input, KeyCode, Label, Layout, Node, ScrollView, Slider, Sprite, SystemEvent, Toggle, Vec2, Vec3, VideoPlayer } from 'cc';
import { UIManager } from '../UIManager';
import { LanauageManager } from '../LanauageManager';
import { GameData } from '../GameData';
import { HttpClient } from '../net/HttpClient';
import { EventManager } from '../EventManager';
import { AudioMgr } from '../AudioMgr';
import { Util } from '../Util';
import { PREVIEW } from 'cc/env';
const { ccclass, property } = _decorator;

@ccclass('main')
export class main extends Component {

    @property(Node)
    bg:Node = null;

    @property(Node)
    selectNodd:Node = null;

    @property(VideoPlayer)
    video:VideoPlayer = null;

    videoUrl:string = "http://hudong.247.hzguanqi.com/static/video/1.mov";

    videoUrl1:string = "http://hudong.247.hzguanqi.com/static/video/2.mp4";

    videoUrl2:string = "http://hudong.247.hzguanqi.com/static/video/3.mp4";

    protected start(): void {

        EventManager.eventTarget.on(EventManager.REFRESH_GAME, this.refresh, this);

        EventManager.eventTarget.on(EventManager.PLAY_VIDEO, this.playVideo, this);

        this.video.node.on(VideoPlayer.EventType.COMPLETED, this.videoComplete, this);

        this.schedule(this.updateTime, 0.8);

        if (PREVIEW){
            input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this);
        }
    }

    onEnable() {
        let videoSelec = localStorage.getItem("step2Video");
        if (!videoSelec){
            GameData.step2Video = 0;
        }else{
            GameData.step2Video = Number(videoSelec);
        }

        let step = localStorage.getItem("curstep");
        if (!step){
            GameData.curstep = 0;
        }else{
            GameData.curstep = Number(step);
        }

        if (GameData.curstep == 0){
            //初始问题未回答
            UIManager.open(UIManager.uiNamePath.popupAnswer);
        }else{
            this.refresh();
        }
    }

    refresh(){
        this.selectNodd.active = false;
        this.bg.active = false;
        if (GameData.curstep == 1){
            this.video.remoteURL = this.videoUrl;
            this.playVideo();
        }
        else if (GameData.curstep == 2){
            this.selectNodd.active = true;
            this.bg.active = true;
            this.selectNodd.children[0].active = true;
            this.selectNodd.children[1].active = true;
        }
        else if (GameData.curstep == 3){
            this.selectNodd.active = true;
            this.bg.active = true;
            if (GameData.step2Video == 1){
                this.selectNodd.children[0].active = false;
                this.selectNodd.children[1].active = true;
            }else{
                this.selectNodd.children[0].active = true;
                this.selectNodd.children[1].active = false;
            }
        }
        else if (GameData.curstep == 4){
            UIManager.open(UIManager.uiNamePath.popupOver);
        }

        localStorage.setItem("curstep", GameData.curstep.toString());
        localStorage.setItem("step2Video", GameData.step2Video.toString());
    }

    playVideo(){
        console.log("恢复播放视频");
        this.video.node.active = true;
        this.selectNodd.active = false;
        this.bg.active = false;
        this.video.play();
    }

    videoComplete(){
        GameData.curstep++;
        this.refresh();
    }

    updateTime(){
        if (GameData.curstep == 1 && this.video.isPlaying){
            let time:number = Math.floor(this.video.currentTime); 
            console.log("视频时间：" + time);
            if (GameData.curVideoCfg && GameData.curVideoCfg.time == time){
                return;
            }
            let cfg = GameData.getVideoConfigByTime(time);
            if (cfg){
                GameData.curVideoCfg = cfg;
                UIManager.open(UIManager.uiNamePath.popupAnswer2);
                this.video.pause();
                // this.video.node.active = false;

            }
        }
    }

    onClickHandler(event:Event, str:string){
        if (str == "btn1"){
            GameData.step2Video = 1
            this.video.remoteURL = this.videoUrl1;
            this.playVideo();
        }
        else if (str == "btn2"){
            GameData.step2Video = 2
            this.video.remoteURL = this.videoUrl2;
            this.playVideo();
        }

    }

    onKeyDown(event: EventKeyboard) {
        // 4. 判断按下的键是否为K键
        if (event.keyCode === KeyCode.KEY_K) {
            console.log('K键被按下了');
            this.videoComplete();
        }
    }

    closeView(){
        UIManager.close(this.node);
    }
}

